<?php
/**
 * tpshop
 * ============================================================================
 * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.tp-shop.cn
 * ----------------------------------------------------------------------------
 * Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
 * ============================================================================
 * Author: 当燃
 * Date: 2015-09-09
 */

namespace Admin\Controller;

use Symfony\Component\Yaml\Dumper;
use Think\AjaxPage;
use Think\Page;
use Admin\Logic\UsersLogic;

class MsgController extends BaseController {




    public function member_list(){

        $this->display();
    }

    /**
     *  列表
     */
    public function ajaxindex(){
        $condition = array();
//        I('mobile') ? $condition['mobile'] = I('mobile') : false;
//        I('email') ? $condition['email'] = I('email') : false;
        $sort_order = 'id ASC';
        $model = M('msg_member');
        $count = $model->where($condition)->count();
        $Page  = new AjaxPage($count,10);
        //  搜索条件下 分页赋值
        foreach($condition as $key=>$val) {
            $Page->parameter[$key]   =   urlencode($val);
        }
        $userList = $model->where($condition)->order($sort_order)->limit($Page->firstRow.','.$Page->listRows)->select();
        $show = $Page->show();
        $this->assign('userList',$userList);
        $this->assign('page',$show);// 赋值分页输出
        $this->display();
    }

    /*******************************************  销售员 **********************************************************/

    /**
     * 添加/修改
     */

    public function addEdit(){
        if(IS_POST){
            $data = I('post.');
            if(!$data['id']){
                $where = "mobile = '$data[mobile]' OR email = '$data[email]'";
                $member =M('msg_member')->where($where)->find();
                if($member)
                    $this->error('该用户已存在！',U('Msg/member_list'));
                $data['add_time']=time();
                $data['status']=1;
                $res =M('msg_member')->add($data);
                if($res){
                    $this->success('添加成功',U('Msg/member_list'));exit;
                }else{
                    $this->error('添加失败,请重试！',U('Msg/member_list'));
                }
            }else{
                $res =M('msg_member')->save($data);
                if($res){
                    $this->success('修改成功',U('Msg/member_list'));exit;
                }else{
                    $this->error('修改失败,请重试！',U('Msg/member_list'));exit;
                }
            }
        }
        $id =I('id');
        if($id){
            $info =M('msg_member')->where("id=$id")->find();
            $this->assign('info',$info);
        }
        $this->display();
    }

    /**
     * 删除
     */
    public function delete(){
        $uid = I('get.id');
        $row = M('msg_member')->where(array('id'=>$uid))->delete();
        if($row){
            $this->success('删除成功');
        }else{
            $this->error('操作失败');
        }
    }



    /**
     *
     * @time 2016/09/03
     * @author dyr
     * 发送邮件
     */
    public function sendMail()
    {
        $user_id_array = I('get.user_id_array');
        $users = array();
        if (!empty($user_id_array)) {
            $user_where = array(
                'user_id' => array('IN', $user_id_array),
                'email' => array('neq', '')
            );
            $users = M('users')->field('user_id,nickname,email')->where($user_where)->select();
        }
        $this->assign('smtp', tpCache('smtp'));
        $this->assign('users', $users);
        $this->display();
    }

    /**
     * 发送邮箱
     * @author dyr
     * @time  2016/09/03
     */
    public function doSendMail()
    {
        $call_back = I('call_back');//回调方法
        $message = I('post.text');//内容
        $title = I('post.title');//标题
        $users = I('post.user');
        if (!empty($users)) {
            $user_id_array = implode(',', $users);
            $users = M('users')->field('email')->where(array('user_id' => array('IN', $user_id_array)))->select();
            $to = array();
            foreach ($users as $user) {
                if (check_email($user['email'])) {
                    $to[] = $user['email'];
                }
            }
            $res = send_email($to, $title, $message);
            echo "<script>parent.{$call_back}({$res});</script>";
            exit();
        }
    }













    /**
     * 等级列表
     */

    public function cateList(){
//        $sales_where['status']=1;

        $cate_list=M('sales_cate')->where($sales_where)->select();

        if(IS_POST){
            $data['name']=I('name');
            if($id=I('id')){
                $data['id']=$id;
                $res =M('sales_cate')->save($data);
            }else{

                $data['add_time']=time();
                $data['status']=1;

                $res =M('sales_cate')->add($data);
            }

            if($res){
                $this->success('操作成功',U('Sales/cateList'));exit;
            }else{
                $this->error('操作失败,请重试！',U('Sales/cateList'));exit;
            }
        }


        if($id=I('id')){
            $sales_info=M('sales_cate')->where("id=$id")->find();
            $this->assign('info',$sales_info);
        }


        $this->assign('lists',$cate_list);
        $this->display();
    }



    /**
     * 删除销售分类
     */



    public function delSalesCate(){
        $id=I('id');
        $sales_num=M('sales')->where("level=$id")->count();
        if($sales_num){
            $this->error('请删除该分类下的销售人员，再行删除该分类');exit;
        }
        $res=M('sales_cate')->where("id=$id")->delete();
        if($res){
            $this->success('删除成功',U('Sales/cateList'));
        }else{
            $this->error('删除失败,请重试！',U('Sales/cateList'));
        }

    }









}